.\" tweak version 3.02
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.TH "tweak" "1" "2004\(hy11\(hy05" "Simon\ Tatham" "Simon\ Tatham"
.SH "NAME"
.PP
\fBtweak\fP - efficient hex editor
.SH "SYNOPSIS"
.PP
.nf
\fBtweak\fP\ [\fB\-l\fP\ |\ \fB\-f\fP]\ [\fB\-e\fP]\ [\fB\-w\fP\ \fIwidth\fP]\ [\fB\-o\fP\ \fIoffset\fP]\ \fIfilename\fP
.fi
.SH "DESCRIPTION"
.PP
\fBtweak\fP is a hex editor. It allows you to edit a file at very low level, letting you see the full and exact binary contents of the file. It can be useful for modifying binary files such as executables, editing disk or CD images, debugging programs that generate binary file formats incorrectly, and many other things.
.PP
Unlike simpler hex editors, \fBtweak\fP possesses a fully functional insert mode. This is not useful when editing many of the types of file described above, but can be useful in other situations. Also, an insert mode makes it easy to use \fBtweak\fP to construct new files from scratch.
.PP
When you open a file in \fBtweak\fP, you can expect to see the screen contents looking something like this:
.PP
.nf
00000000\ \ \ 7F\ 45\ 4C\ 46\ 01\ 01\ 01\ 00\ \ \ .ELF....
00000008\ \ \ 00\ 00\ 00\ 00\ 00\ 00\ 00\ 00\ \ \ ........
00000010\ \ \ 02\ 00\ 03\ 00\ 01\ 00\ 00\ 00\ \ \ ........
00000018\ \ \ D0\ 8E\ 04\ 08\ 34\ 00\ 00\ 00\ \ \ ....4...
00000020\ \ \ 2C\ EF\ 01\ 00\ 00\ 00\ 00\ 00\ \ \ ,.......
.fi
.PP
The central column shows you the hexadecimal value of each byte in the file you are editing. The column on the right shows the ASCII interpretation of those bytes, where applicable. In the example above, the sequence \fB45 4C 46\fP on the first line translates into the ASCII upper-case letters `ELF', but the subsequent sequence \fB01 01 01 00\fP does not have any printable ASCII representation and so the right-hand column simply prints dots.
.PP
The column on the left shows the position within the file of the start of each row.
.PP
In fact, when you start \fBtweak\fP, you will usually see 16 bytes of the file per row, not 8 as shown above. However, this is configurable if your screen is narrower - or wider - than the usual 80 columns, or if the file you are editing consists of fixed-size records of some other size.
.PP
By default, \fBtweak\fP does not load its entire input file into memory. Instead, it loads it \fIlazily\fP, reading from the file on disk when you request a view of a part of the file it doesn\*(Aqt have stored. When you modify the file, it stores your modifications in memory, but continues to refer to the original disk file for the parts you have not touched. This means you can edit extremely large files (for example, entire CD images) without difficulty; opening such a file is instantaneous, making modifications causes \fBtweak\fP\*(Aqs memory usage to grow with the size of the changes rather than the size of the whole file, and only when saving the altered version will \fBtweak\fP have to read through the entire input file to write the output.
.PP
However, this mode of operation has a disadvantage, which is that if the input file is modified by another program while \fBtweak\fP is running, \fBtweak\fP\*(Aqs internal data structures will not be sufficient to keep track, and it is likely that the file written out will contain a mixture of the old and new contents of the input file. Therefore, you can disable this lazy loading if you need to; see the \fB-e\fP option below.
.SH "OPTIONS"
.PP
This section lists the command-line options supported by \fBtweak\fP.
.IP "\fB-f\fP"
Runs \fBtweak\fP in `fix' mode, i.e. with the insert function entirely disabled. This might be useful if you are editing a file in which the insert mode is of no use (executables, for example, tend to have strong dependencies on precise file offsets which make it almost impossible to insert data in one without rendering it unusable) and you want to avoid turning it on by mistake.
.IP "\fB-l\fP"
Runs \fBtweak\fP in `look' mode. In this mode \fBtweak\fP does not allow you to modify the data at all; it becomes simply a tool for examining a file in detail.
.IP "\fB-e\fP"
Runs \fBtweak\fP in `eager' mode. In this mode \fBtweak\fP will read its entire input file when starting up. This causes it to take up more memory, but means that it has no dependency on the input file remaining unmodified, and other programs can alter it if they need to without causing trouble.
.IP "\fB-w\fP \fIwidth\fP"
Specifies the number of bytes \fBtweak\fP will display per line. The default is 16, which fits neatly in an 80-column screen.
.IP "\fB-o\fP \fIoffset\fP"
If this option is specified, \fBtweak\fP will ensure that the given file offset occurs at the start of a line. For example, if you loaded a file using the options \fB-w 8 -o 0x13\fP, you might see a display a bit like this:
.RS
.PP
.nf
00000000\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 7F\ 45\ 4C\ \ \ \ \ \ \ \ .EL
00000003\ \ \ 46\ 01\ 01\ 01\ 00\ 00\ 00\ 00\ \ \ F.......
0000000B\ \ \ 00\ 00\ 00\ 00\ 00\ 02\ 00\ 03\ \ \ ........
00000013\ \ \ 00\ 01\ 00\ 00\ 00\ D0\ 8E\ 04\ \ \ ........
0000001B\ \ \ 08\ 34\ 00\ 00\ 00\ 2C\ EF\ 01\ \ \ .4...,..
.fi
.PP
By putting only three bytes of the file on the very first line, \fBtweak\fP has arranged that the file offset 0x13 (19 in decimal) appears at the beginning of the fourth line.
.PP
You might use this option if you knew you were editing a file in a particular format. For example, if your file contained a 53-byte header followed by a series of 22-byte records, you might find it useful to specify the options \fB-w 22 -o 53\fP. This would arrange that after the header, each individual record of the file would appear on precisely one line of \fBtweak\fP\*(Aqs display.
.RE
.IP "\fB-D\fP"
If this option is specified, \fBtweak\fP will not attempt to load and edit a file at all, but will simply produce its default \fB.tweakrc\fP file on standard output. This is a useful way to give yourself a starting point if you want to begin reconfiguring \fBtweak\fP\*(Aqs keyboard layout.
.SH "KEYS"
.PP
This section describes all the editing keys supported by \fBtweak\fP by default. The default key bindings for \fBtweak\fP are basically Emacs-like.
.SS "Movement keys"
.PP
The Emacs cursor movement keys should all work, and their counterparts in ordinary function keys ought to work too:
.IP "\fB\(bu\fP"
\fB^P\fP and \fB^N\fP go to the previous and next lines; Up and Down should do the same.
.IP "\fB\(bu\fP"
\fB^B\fP and \fB^F\fP go back and forward one character; Left and Right should do the same.
.IP "\fB\(bu\fP"
\fBM-v\fP and \fB^V\fP go up and down one screenful at a time; Page Up and Page Down should do the same.
.IP "\fB\(bu\fP"
\fB^A\fP and \fB^E\fP go to the beginning and end of the line; Home and End should do the same.
.PP
Press \fBM-<\fP and \fBM->\fP go to the beginning and end of the file.
.PP
Press \fB^X g\fP to go to a particular byte position in the file; you will be asked to type in the position you want. You can enter it in decimal, or as a hex number with `\fB0x\fP' before it.
.SS "Editing keys"
.PP
Press Return to move the cursor between the hex section of the screen and the ASCII section.
.PP
When in the hex section, you can enter hexadecimal digits to alter data; when in the ASCII section, you can directly type ASCII text.
.PP
In ASCII mode, you can also press \fB^Q\fP to literally quote the next input character; for example, if you want to insert a Control-V, you can press \fB^Q^V\fP and \fBtweak\fP will automatically insert the byte value 0x16.
.PP
Press \fB^X^I\fP, or the Insert key if you have one, to toggle between overwrite mode and insert mode. In insert mode, typing hex or ASCII input will insert new bytes containing the values you provide. Also, you can then press Backspace to delete the byte to the left of the cursor, or \fB^D\fP or Delete to delete the byte under the cursor.
.SS "Cut and paste"
.PP
Press \fB^@\fP (this character may be generated by the key combination Control-@, or Control-2, or Control-Space) to mark the end of a selection region. After you do this, the bytes between that mark and the cursor will be highlighted. Press \fB^@\fP again to abandon the selection.
.PP
Press \fBM-w\fP while a selection is active to copy the selected region into \fBtweak\fP\*(Aqs cut buffer.
.PP
In insert mode, you also have the option of pressing \fB^W\fP to \fIcut\fP the selected region completely out of the file and place it in the cut buffer.
.PP
Finally, press \fB^Y\fP to paste the cut buffer contents back into the file (this will overwrite or insert depending on the current mode).
.SS "Searching"
.PP
Press \fB^S\fP to search for a byte sequence. You will be asked to enter some text to search for on the bottom line of the screen. You can type this text in ASCII, or as a sequence of hex byte values prefixed with backslashes (\fB\e\fP). For example, if you wanted to search for the byte value 5 followed by the word `hello', you might enter \fB\e05hello\fP. If you want to specify a literal backslash character, you can either enter it in hex (as \fB\e5C\fP), or simply double it on input (\fB\e\e\fP).
.PP
Press \fB^R\fP to search backwards instead of forwards from the current cursor position.
.PP
Since \fBtweak\fP deals in pure binary data, searches are always case-sensitive.
.SS "Controlling the display"
.PP
If you press \fB^X w\fP, you will be asked to enter a new display width. This has the same effect as passing the \fB-w\fP option on the command line. Similarly, pressing \fB^X o\fP allows you to enter a new display offset, equivalent to the \fB-o\fP option.
.PP
By default, the current file position and file size are displayed on \fBtweak\fP\*(Aqs status line in hex. If you prefer them in decimal, you can press \fB^X x\fP or \fB^X h\fP to toggle them between hex and decimal.
.SS "Miscellaneous"
.PP
Press \fB^L\fP to redraw the screen and recentre the cursor. Press \fB^Z\fP to suspend \fBtweak\fP and return temporarily to the shell.
.PP
Press \fB^X^S\fP to save the file you are editing.
.PP
Press \fB^X^C\fP to exit \fBtweak\fP. (If you do this with changes unsaved, you will be asked whether you want to save them.)
.SH "CONFIGURATION FILE"
.PP
\fBtweak\fP\*(Aqs keyboard bindings are configurable. It will attempt to read a file from your home directory called \fB.tweakrc\fP, and if it finds one it will use the keyboard bindings described in it. If it does not find one, it will use its internal default bindings.
.PP
Most of the directives in \fB.tweakrc\fP are of the form `\fBbind command-name key\fP'. For example, `\fBbind exit ^X^C\fP'. Additionally, there are two other directives, \fBwidth\fP and \fBoffset\fP, which give the default display parameters if no \fB-w\fP and \fB-o\fP options are specified.
.PP
The easiest way to learn about the \fB.tweakrc\fP file is to begin by having \fBtweak\fP output its internal default one:
.PP
.nf
tweak\ \-D\ >\ $HOME/.tweakrc
.fi
.PP
Then you can read the default file, learn the \fBtweak\fP internal command names, and edit the file to do what you want.
.SH "BUGS"
.PP
This man page probably ought to contain an explicit list of internal command names, rather than simply referring you to the default \fB.tweakrc\fP.
